package com.unlcn.ils.kas.controller.order;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.unlcn.ils.kas.dao.model.order.OrderOtd;
import com.unlcn.ils.kas.dao.model.order.bo.OrderLineBo;
import com.unlcn.ils.kas.service.order.OrderExceptionService;
import com.unlcn.ils.kas.service.util.enums.CommonExceptionEnum;
import com.unlcn.ils.kas.service.util.enums.order.OrderExceptionClassEnum;
import com.unlcn.ils.kas.vo.RestVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/order/exception")
public class OrderExceptionController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderExceptionController.class);

    @Autowired
    private OrderExceptionService orderExceptionService;

    @GetMapping(value = "/getClass")
    public RestVO getClass(String sysOrderNo) {

        RestVO restVO = null;

        try {

            List<Map<String, Object>> list = new ArrayList<>();

            OrderExceptionClassEnum[] orderExceptionClassEnums = OrderExceptionClassEnum.values();
            for (OrderExceptionClassEnum orderExceptionClassEnum : orderExceptionClassEnums) {

                Map<String, Object> map = new HashMap<>();

                map.put("code", orderExceptionClassEnum.getCode());
                map.put("text", orderExceptionClassEnum.getText());

                list.add(map);
            }

            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), list, CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderExceptionController.getClass ERROR Message : {} ", e);
            restVO = new RestVO(CommonExceptionEnum.ERROR.getCode(), CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @PostMapping(value = "/exceptionLine")
    public RestVO<OrderLineBo> exceptionLine(Page<OrderOtd> page) {
        LOGGER.error("OrderExceptionController.exceptionLine（获取异常订单列表） params : {} ", JSON.toJSONString(page));

        RestVO<OrderLineBo> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderExceptionService.exceptionLine(page), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderExceptionController.exceptionLine ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @PostMapping(value = "/pullOrder")
    public RestVO pullOrder(String sysOrderNo) {

        RestVO restVO = null;

        try {

            if (StringUtils.isEmpty(sysOrderNo)) {
                restVO = new RestVO(CommonExceptionEnum.ERROR.getCode(), "参数sysOrderNo不能为空, 多个要以,分隔");
                return restVO;
            }

            List<Map<String, String>> result = orderExceptionService.pullOrder(sysOrderNo.split(","));

            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), JSON.toJSONString(result));
        } catch (Exception e) {
            LOGGER.error("OrderExceptionController.pullOrder ERROR Message : {} ", e);
            restVO = new RestVO(CommonExceptionEnum.ERROR.getCode(), CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }
}
